package com.wetalk.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class JwtUtils {

    public static String jwt;

    //HS256签名算法
    public static String jwtCreate(Map<String,Object> cliams){
        String mymessage = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "myMessage")//签名算法&密钥
                .setClaims(cliams)//传输内容
                .setExpiration(new Date(System.currentTimeMillis() + 10*60*1000))//有效期，时间毫秒
                .compact();
        return mymessage;
    }

    public static Claims jwtRead(String secretNum){
        Claims claim =Jwts.parser()
                .setSigningKey("myMessage")//密钥
                .parseClaimsJws(secretNum)
                .getBody();
        jwt=secretNum;
        return claim;
    }

    public static void main(String[] args) {
        Map<String, Object> cliams = new HashMap<>();
        cliams.put("name","12");
        cliams.put("password","12");
        String mymessage = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "myMessage")//签名算法&密钥
                .setClaims(cliams)//传输内容
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 3600))//有效期，时间毫秒
                .compact();
        System.out.println(mymessage);
    }


}
